﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.International.Converters.PinYinConverter;

namespace DoNetExt
{
    public static class StringExt
    {
        public static string ToOnlyNumber(this string iString)
        {
            char[] CharList = iString.ToCharArray();
            return new string(CharList.Where(p => char.IsNumber(p)).ToArray());
        }
        public static string ToNotNumber(this string iString)
        {
            char[] CharList = iString.ToCharArray();
            return new string(CharList.Where(p => char.IsNumber(p) == false).ToArray());
        }

        public static string InitialToUpper(this string iString)
        {
            iString = iString.ToLower();
            char[] CharList = iString.ToCharArray();
            int? Index = CharList.Select((p, index) => new { Index = index, Value = p }).Where(p => p.Value.IsAlpah()).Select(p => p.Index).FirstOrDefault();
            if (Index != null)
            {
                int IIndex = (int)Index;
                char Ch = CharList[IIndex];
                Ch = Ch.ToString().ToUpper().ToCharArray().First();
                CharList[IIndex] = Ch;
                return new string(CharList);
            }
            else
            {
                return iString;
            }
        }
        public static string ToPinyin(this string iString)
        {
            string PinYinStr = "";
            var CharArray = iString.ToCharArray();
            foreach (char Ch in CharArray)
            {
                if (ChineseChar.IsValidChar(Ch))
                {
                    ChineseChar iChineseChar = new ChineseChar(Ch);
                    string PinyinWord = iChineseChar.Pinyins[0];
                    PinYinStr += PinyinWord.InitialToUpper().ToNotNumber();
                }
            }
            return PinYinStr;
        }
        public static string ToPinyinInitial(this string iString)
        {
            string PinYinStr = "";
            var CharArray = iString.ToCharArray();
            foreach (char Ch in CharArray)
            {
                if (ChineseChar.IsValidChar(Ch))
                {
                    ChineseChar iChineseChar = new ChineseChar(Ch);
                    string PinyinWord = iChineseChar.Pinyins[0];
                    PinYinStr += PinyinWord.InitialToUpper().ToNotNumber().Substring(0, 1).ToLower();
                }
            }
            return PinYinStr;
        }
        public static string ToTrimLower(this string iString)
        {
            return iString.Trim().ToLower();
        }
        public static bool IsNullOrWhiteSpace(this string iString)
        {
            return string.IsNullOrWhiteSpace(iString);
        }
        public static bool ContainsLower(this string iString, string SubString)
        {
            return iString.ToTrimLower().Contains(SubString.ToTrimLower());
        }
    }
}
